package com.cmnit.service;

import com.alibaba.fastjson.JSONArray;
import com.cmnit.bean.DangerousTruckBean;
import com.cmnit.bean.DangerousTruckPostBean;
import com.cmnit.dao.DangerousTruckDao;
import com.cmnit.utils.ConfigurationManager;
import org.apache.log4j.Logger;

import java.util.List;

public class DangerousTruckService {
    private static final Logger log = Logger.getLogger(DangerousTruckService.class);
    private DangerousTruckDao dao = new DangerousTruckDao();
    private DangerousTruckPostBean postBean = new DangerousTruckPostBean();

    public void service() {
        // 获取当前时间
        long time = System.currentTimeMillis();

        // post类加载接口调用信息
        postBean.setTs(time);
        postBean.setAk(ConfigurationManager.getProperty("http.post.ak"));
        postBean.setSk(ConfigurationManager.getProperty("http.post.sk"));
        postBean.setPage(ConfigurationManager.getProperty("http.post.page"));
        postBean.setLimit(ConfigurationManager.getProperty("http.post.limit"));

        // 获取总页数
        int pages = dao.getPageInfo(postBean.mkKVPair());
        // 判断是否存在数据
        if (pages >= ConfigurationManager.getIntProperty("dangerousTruck.page.min")) {
            // 清理原表数据
            dao.deleteRecords();

            // 循环获取接口数据
            for (int i = 1; i <= pages; i++) {
                // 设置页数
                postBean.setPage(String.valueOf(i));
                // 获取数据
                String records = dao.getRecords(postBean.mkKVPair());
                // 解析JSON数组
                List<DangerousTruckBean> dangerousTruckList = JSONArray.parseArray(records, DangerousTruckBean.class);
                // 写入Oracle
                dao.updateRecords(dangerousTruckList);
            }
            log.info("数据写入完毕");
        }
    }
}
